﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Models;

namespace DAL.TWTZ
{
    /// <summary>
    /// 体温台账
    /// </summary>
    public class TwtzDal
    {
        private SqlDbHelper sqldbhelper = new SqlDbHelper();
        #region 新增及更新体温台账信息
        /// <summary>
        /// 新增及更新体温台账信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int SaveTwtz(TwtzModel entity)
        {
            int returnVal = 0;
            try
            {
                using (IDbConnection connect = new SqlConnection(sqldbhelper.strCon))
                {
                    string sql = string.Empty;

                    TwtzModel oldEntity = GetEntity(entity.TwtzId);
                    //新增
                    if (oldEntity == null)
                    {
                        //sql = @"insert into Unit(UnitId,UnitName,UnitCode,SequenceCode,ParentId,UnitType,Province,City,Remark,SortCode) 
                        //     values(@UnitId,@UnitName,@UnitCode,@SequenceCode,@ParentId,@UnitType,@Province,@City,@Remark,@SortCode)";
                        sql = @"insert into twtz(twtzid,unitId,UnitName,UnitCode,recordpersonname,
                                recordpersonid,totalperson,normalperson,abnormalperson,Operatetime,Remark) values 
                                (@twtzid,@unitId,@UnitName,@UnitCode,@recordpersonname,
                          @recordpersonid,@totalperson,@normalperson,@abnormalperson,@Operatetime,@Remark)";
                    }
                    else //更新
                    {
                        sql = @"update twtz set unitId=@unitId,UnitName=@UnitName,UnitCode=@UnitCode,recordpersonname=@recordpersonname,
                                recordpersonid=@recordpersonid,totalperson=@totalperson,normalperson=@normalperson,abnormalperson=@abnormalperson,
                                Operatetime=@Operatetime,Remark=@Remark where twtzid=@twtzid";
                    }
                    //    SqlParameter[] param = new SqlParameter[] {
                    //    new SqlParameter("@UnitId",entity.UnitId),//主键id
                    //    new SqlParameter("@UnitName",entity.UnitName), //单位名称
                    //    new SqlParameter("@UnitCode",entity.UnitCode), //单位编码
                    //    new SqlParameter("@SequenceCode",entity.TwtzId), //序列号
                    //    new SqlParameter("@ParentId",entity.TwtzId), //父节点id
                    //    new SqlParameter("@UnitType",entity.TwtzId),//单位类型
                    //    new SqlParameter("@Province",entity.TwtzId),//省份
                    //    new SqlParameter("@City",entity.TwtzId),//城市
                    //    new SqlParameter("@Remark",entity.Remark), //备注说明
                    //    new SqlParameter("@SortCode",entity.TwtzId) //排序号
                    //};
                    SqlParameter[] param = new SqlParameter[] {
                    new SqlParameter("@twtzid",entity.TwtzId),
                    new SqlParameter("@unitId",entity.UnitId),
                    new SqlParameter("@UnitName",entity.UnitName),
                    new SqlParameter("@UnitCode",entity.UnitCode),
                    new SqlParameter("@recordpersonname",entity.RecordPersonName),
                    new SqlParameter("@recordpersonid",entity.RecordPersonId),
                    new SqlParameter("@totalperson",entity.TotalPerson),
                    new SqlParameter("@normalperson",entity.NormalPerson),
                    new SqlParameter("@abnormalperson",entity.AbnormalPerson),
                    new SqlParameter("@Operatetime",entity.OperateTime),
                    new SqlParameter("@Remark",entity.Remark)
                    };
                    returnVal = connect.Execute(sql, new
                    {
                        twtzid = entity.TwtzId,
                        unitId = entity.UnitId,
                        UnitName = entity.UnitName,
                        UnitCode = entity.UnitCode,
                        recordpersonname = entity.RecordPersonName,
                        recordpersonid = entity.RecordPersonId,
                        totalperson = entity.TotalPerson,
                        normalperson = entity.NormalPerson,
                        abnormalperson = entity.AbnormalPerson,
                        Operatetime = entity.OperateTime,
                        Remark = entity.Remark
                    });
                    sql = "delete TemperaturePerson where TwtzId=@TwtzId";
                    param = new SqlParameter[] {
                        new SqlParameter("@TwtzId",entity.TwtzId)
                    };
                    returnVal = connect.Execute(sql, new { TwtzId= entity.TwtzId });
                    foreach (var item in entity.TemperaturePerson)
                    {
                        item.Id = Guid.NewGuid().ToString();
                        item.TwtzId = entity.TwtzId;
                        if ((double)item.Temperature > 37.2)
                        {
                            TemperatureHighModel thm = new TemperatureHighModel();
                            thm.DepartId = item.UnitId;
                            thm.DepartName = item.UnitName;
                            thm.userName = item.TemperaturePersonName;
                            thm.userId = item.TemperaturePersonId;
                            thm.BodyCondition = "";
                            thm.DepartmentName = "";
                            thm.Temperature = (float)item.Temperature;
                            thm.DealInfo = "";
                            thm.RegistDate = entity.OperateTime;//录入时间
                            thm.CreateUserAcount = "";
                            thm.CreateTime = DateTime.Now;
                            thm.UpdateTime = DateTime.Now;
                            if (new TemperatureHighDal().InsertSingleData(thm) == false)
                            {
                                throw new Exception();
                            }
                        }
                    }
                    sql = @"insert into TemperaturePerson(Id,UnitId,UnitName,UnitCode,TemperaturePersonName,TemperaturePersonId,Temperature,TwtzId)
                            values(@Id,@UnitId,@UnitName,@UnitCode,@TemperaturePersonName,@TemperaturePersonId,@Temperature,@TwtzId)";
                    returnVal = connect.Execute(sql, entity.TemperaturePerson);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnVal;
        }
        #endregion

        #region 删除体温台账信息
        /// <summary>
        /// 删除体温台账信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int RemoveTwtz(string id)
        {
            int returnVal = 0;
            try
            {
                using (IDbConnection connect = new SqlConnection(sqldbhelper.strCon))
                {
                    string sql = @"delete twtz  where  twtzId=@twtzId";
                    SqlParameter[] param = new SqlParameter[]
                    {
                        new SqlParameter("@twtzId",id)
                    };
                    returnVal = connect.Execute(sql, param);
                }
            }
            catch (Exception)
            {
                return -1;
            }
            return returnVal;
        }
        #endregion

        #region 获取体温台账对象
        /// <summary>
        /// 获取体温台账对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TwtzModel GetEntity(string id)
        {
            using (IDbConnection connection = new SqlConnection(sqldbhelper.strCon))
            {
                return connection.Query<TwtzModel>(@"select  * from twtz where twtzid=@twtzid", new { twtzid = id }).SingleOrDefault();
            }
        }
        #endregion

        #region 获取体温台账对象的测温人员
        /// <summary>
        /// 获取体温台账对象的测温人员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<TemperaturePerson> GetTemperaturePersonList(string id)
        {
            using (IDbConnection connection = new SqlConnection(sqldbhelper.strCon))
            {
                return connection.Query<TemperaturePerson>(@"select  * from TemperaturePerson where twtzid=@twtzid", new { twtzid = id }).ToList();
            }
        }
        #endregion

        #region 根据单位获取测温人员
        public int GetTemperaturePersonList(string unitid,string persontype)
        {
            try
            {
                using (IDbConnection connection = new SqlConnection(sqldbhelper.strCon))
                {
                    DataTable dt = new DataTable();
                    string sql = @"select distinct b.TemperaturePersonId from twtz a left join TemperaturePerson b on cast(a.twtzid as nvarchar(64))=b.twtzid
                        left join employee c on b.TemperaturePersonId=c.EmployeeId
                    where a.unitid=@unitid and c.employeetype=@employeetype and a.operatetime between 
                        CONVERT(varchar(100), GETDATE(), 23) + ' 00:00:00' AND CONVERT(varchar(100), GETDATE(), 23) +' 23:59:59'";
                    DynamicParameters param = new DynamicParameters();
                    param.Add("@unitid", unitid, DbType.String);
                    param.Add("@employeetype", persontype, DbType.String);
                    var Reader = connection.ExecuteReader(sql, param: param);
                    dt.Load(Reader);
                    return dt.Rows.Count;
                }
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 获取当前单位下的子集合
        /// <summary>
        /// 获取当前节点下的子集合
        /// </summary>
        /// <param name="UnitId"></param>
        /// <returns></returns>
        public List<TwtzModel> GetListByUnitId(string UnitId)
        {
            List<TwtzModel> list = new List<TwtzModel>();
            try
            {
                string sql = @"select * from twtz where unitid = @UnitId ";
                using (IDbConnection connection = new SqlConnection(sqldbhelper.strCon))
                {
                    list = connection.Query<TwtzModel>(sql, UnitId).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return list;
        }
        #endregion

        #region 获取所有体温台账信息
        /// <summary>
        /// 获取所有体温台账信息
        /// </summary>
        /// <returns></returns>
        public List<TwtzModel> GetList()
        {
            List<TwtzModel> list = new List<TwtzModel>();
            try
            {
                string sql = "select * from twtz";
                using (IDbConnection connection = new SqlConnection(sqldbhelper.strCon))
                {
                    list = connection.Query<TwtzModel>(sql).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return list;
        }
        #endregion
    }
}
